import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import Qt.labs.platform 1.1
import QtQuick.Controls.Material

Item {
    Layout.fillHeight: true
    Layout.preferredWidth: 400

    property alias historyModel: historyList.model

    signal clearHistoryClicked()
    signal exportHistoryClicked()
    signal itemClicked(var historyItem)

    Pane {
        anchors.fill: parent
        padding: 20

        ColumnLayout {
            anchors.fill: parent
            spacing: 16

            Label {
                text: qsTr("历史记录 (History)")
                font.bold: true
            }

            ScrollView {
                Layout.fillWidth: true
                Layout.fillHeight: true
                ScrollBar.vertical.policy: ScrollBar.AlwaysOn

                ListView {
                    id: historyList
                    Layout.fillWidth: true
                    Layout.fillHeight: true
                    spacing: 10
                    clip: true

                    delegate: Pane {
                        width: ListView.view.width
                        implicitHeight: historyItemLayout.implicitHeight + 30
                        padding: 15
                        Material.elevation: 2

                        ColumnLayout {
                            id: historyItemLayout
                            anchors.fill: parent
                            spacing: 5

                            Label {
                                Layout.fillWidth: true
                                text: model.prompt
                                wrapMode: Text.WordWrap
                                font.bold: true
                            }
                            Label {
                                Layout.fillWidth: true
                                text: model.timestamp
                                font.pointSize: 9
                                color: Material.secondaryTextColor
                            }
                        }
                        MouseArea {
                            anchors.fill: parent
                            onClicked: itemClicked(model)
                        }
                    }
                }
            }

            RowLayout {
                Layout.alignment: Qt.AlignHCenter
                spacing: 10

                Button {
                    text: qsTr("清空历史")
                    onClicked: clearHistoryClicked()
                }

                Button {
                    text: qsTr("导出为 TXT")
                    onClicked: exportHistoryClicked()
                }
            }
        }
    }
}